package in.muton.edushare.components.tasks.electives;

import in.muton.edushare.components.tasks.electives.api.SerialChecker;
import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.pages.common.EdusharePanel;
import in.muton.edushare.pages.coursepages.CoursePage;
import in.muton.edushare.url.EntityPageParams;
import in.muton.edushare.wicket.models.CayenneList;

import java.util.HashMap;
import java.util.Set;
import java.util.Map.Entry;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import com.edushare.common.constants.ApplicationConstants;
import com.edushare.common.dao.CurriculumDAO;
import com.edushare.common.dao.ElectivePreferenceDAO;
import com.edushare.common.vo.Courses;
import com.edushare.common.vo.Curricula;
import com.edushare.common.vo.Universities;
import com.edushare.common.vo.Users;

public class UniqueElectivePanel extends EdusharePanel {
	private static final long serialVersionUID = 1450942849841502230L;

	public UniqueElectivePanel(String id, EduShareBasePage page, final IModel<Curricula> currmodel,
			IModel<Users> userModel, final Integer currSemester,
			final Integer groupId) {
		super(id, page);
		this.currmodel = currmodel;
		this.userModel = userModel;
		this.currSemester = currSemester;
		this.groupId = groupId;

		CurriculumDAO cDAO = new CurriculumDAO(getORMInterface());
		CayenneList<Courses> courseList = new CayenneList<Courses>(cDAO
				.getElectiveCourses(currmodel.getObject(), currSemester,
						groupId));
		
		
		Form<Void> f;
		add(f = new Form<Void>("mainForm"){
			@Override
			public void onSubmit(){
				SerialChecker sc = new SerialChecker();
				sc.checkElectivePreference(map);
				if(sc.isError()){
					setError(sc.getErrorMessage());
					return;
				}else{
					setError("");
				}
				
				Set<Entry<IModel<Courses>, TextField<String>>> set = map.entrySet();
				if (currmodel.getObject().getUniversity()
						.getElectiveSelectionTime() != Universities.UNIV_ALLOW_ELECTIVE_SELECTION) {
					setError("Elective choice selection has been closed by the university administration.<br/>"
							+ ApplicationConstants.APPLICATION_NAME
							+ " will not be able to save your choices");
					return;
				}
				ElectivePreferenceDAO eDAO = new ElectivePreferenceDAO(
						getORMInterface());
				CurriculumDAO cDAO = new CurriculumDAO(
						getORMInterface());
				for(Entry<IModel<Courses>, TextField<String>> s:set){
					eDAO.addSetOfPreferences(getLoggedInUser(), cDAO
							.getSemCourse(currmodel.getObject(),
									currSemester, groupId, s.getKey()
											.getObject()),
							groupId, Integer.parseInt(s.getValue().getModelObject()));
				}
			}
			private static final long serialVersionUID = -4571224984334429744L;
		});
		f.add(new Label("electiveGroup","Elective - "+ (groupId+1)  + " of "
				+ currmodel.getObject().getCurriculaName() +" for semester "+ (currSemester+1) +"."));
		
		
		
		f.add(new ListView<IModel<Courses>>("choiceListLV", courseList) {
			@Override
			protected void populateItem(final ListItem<IModel<Courses>> arg0) {
				Link<Void> l;
				arg0.add(new Label("serialNo",Integer.toString(arg0.getIndex()+1)));
				arg0.add(l = new BookmarkablePageLink<Void>("courseLink",
						CoursePage.class, EntityPageParams.getParams(arg0
								.getModelObject())));
				l.add(new Label("courseName", arg0.getModelObject().getObject()
						.getCourseName()));
				TextField<String> tf;
				
				arg0.add(tf = new TextField<String>("preference", new Model<String>(){
					@Override
					public void setObject(String str){
						s = str;
					}
					@Override
					public String getObject(){
						return s;
					}
					private String s = ""+(arg0.getIndex()+1);
					private static final long serialVersionUID = 2382024218734159353L;
				}));
				map.put(arg0.getModelObject(),tf);
			}
			private static final long serialVersionUID = -4006228290877116122L;
		});

	}

	HashMap<IModel<Courses>, TextField<String>> map = new HashMap<IModel<Courses>, TextField<String>>();
	IModel<Curricula> currmodel;
	IModel<Users> userModel;
	Integer currSemester;
	Integer groupId;
}
